<?php
$path =  str_replace('/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stPublishToolbox/cronjob','',dirname(__FILE__));
require($path.'/wp-config.php');
$config = include $path.'/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stPublishToolbox/config.php';
require($path.'/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stPublishToolbox/facebook/facebook.php');
global $wpdb;
define('APP_API_KEY', $config['facebook_api_key']);
define('APP_SECRET_KEY', $config['facebook_secret']);
define('APP_ID', $config['facebook_app_id']);
$option  = get_option("wall_publish_option");
$facebook = new Facebook(APP_API_KEY, APP_SECRET_KEY, $option['session_key']);
$facebook->set_user($option['uid'], $option['session_key']);
$attachment = null;
$data['cronjob_name']  = 'cronjob Publish';
$data['toolbox_name'] = 'stPublishToolbox';
$data['url'] = dirname(__FILE__).'/wall_publish.php';
$data['status'] = 0;
$data['ctime'] = date_i18n('Y-m-d H:i:S');
$wpdb->insert('wp_st_cronjob_log',$data);
$log_id = $wpdb->get_var("SELECT MAX(id) FROM wp_st_cronjob_log");
foreach($wpdb->get_results("SELECT * FROM st_publishing WHERE published=1 AND publish_type='facebook'") as $row){
    $date_array = explode(' ',$row->time);
    $date = explode('-',$date_array[0]);
    $time = explode(':',$date_array[1]);
    $current =  mktime ($time[0],$time[1],00 ,intval($date[1]) ,intval($date[0]) ,$date[2] );
    if($current <= mktime(date_i18n("H"),date_i18n("i"),date_i18n("s"),date_i18n("n"),date_i18n("j"),date_i18n("Y"))){
                 if($row->image_upload != ''){
                     $image = explode('/wp-content/',$row->image_upload);
                     $image = get_bloginfo('url').'/wp-content/'.$image[1];
                }
                $attachment = null;
                if($row->type == 'image'){
                    if($row->image_type == 0)
                    $attachment = array( 'name' => stripslashes($row->name), 'media' => array(array('type' => 'image', 'src' => $row->thumb, 'href' => $row->thumb)));
                    else
                    $attachment = array( 'name' => stripslashes($row->name), 'media' => array(array('type' => 'image', 'src' => $image, 'href' => $image)));
                }
                elseif($row->type == 'video'){
                    $attachment = array( 'name' => stripslashes($row->name), 'href' => stripslashes($row->media), 'description' => '','media' => array(array('type' => 'flash', 'swfsrc' => $row->media, 'imgsrc' => $row->thumb)));
               }
               elseif($row->type == 'swf' || $row->type == 'slideshare'){
                   $attachment = array('name' => stripslashes($row->name),'description' => stripslashes(''),'media' => array(array('type' => 'flash','swfsrc' => $row->media,'imgsrc' => $row->thumb)));
               }
                foreach(json_decode($row->page) as $pa){
                    if($facebook->api_client->pages_isAdmin($pa,$option['uid'])){
                       $facebook->api_client->stream_publish($row->content, $attachment, null,null,$pa);
                       }
                    else
                    {
                      $facebook->api_client->stream_publish($row->content, $attachment, null,$pa);
                    }
                }
                $wpdb->update('st_publishing',array('published'=>0),array('id'=>$row->id));
    }
}
$wpdb->update('wp_st_cronjob_log',array('status'=>1),array('id'=>$log_id));
?>
